﻿using System;
using System.Web.Mvc;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;
using iss.sg4210.se18.team6s.Utilities;
using iss.sg4210.se18.team6s.jass.BL;

namespace iss.sg4210.se18.team6s.Controllers
{
    public class EventController : Controller
    {
        PublishingsFacade publishingFacade = null;
        public EventController()
        {
            publishingFacade = new PublishingsFacade();
        }
        //
        // GET: /Event/

        public ActionResult Index()
        {
            if (Common.IsLoggedInUserAdmin())
                return View(publishingFacade.FindAllEvents());
            else
                return View(publishingFacade.FindAllLatestEvents());
        }

        //
        // GET: /Event/Details/5

        public ActionResult Details(int id)
        {
            return View(publishingFacade.GetEvent(id));
        }

        //
        // GET: /Event/Create
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Event/Create

        [HttpPost]
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Event events = new Event();
                UpdateModel(events, collection);
                events.UserId = Common.GetLoggedInUserId();
                Session["Event"] = events;

                return RedirectToAction("CreateAddress");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Event/CreateAddress
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult CreateAddress()
        {
            return View();
        }

        //
        // POST: /Event/CreateAddress

        [HttpPost]
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult CreateAddress(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Address address = new Address();
                UpdateModel(address, collection);

                Event events = (Event)Session["Event"];
                publishingFacade.AddEvent(events, address);

                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Event/Edit/5
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Edit(int id)
        {
            return View(publishingFacade.GetEvent(id));
        }

        //
        // POST: /Event/Edit/5

        [HttpPost]
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Event events = publishingFacade.GetEvent(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(events);

                Session["Event"] = events;
                Session["EventAddress"] = events.Address;

                return RedirectToAction("EditAddress");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(events);
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message);
                return View(events);
            }
        }

        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult EditAddress()
        {
            Address eventAddress = (Address)Session["EventAddress"];
            return View(eventAddress);
        }

        [HttpPost]
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult EditAddress(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                PublishingsFacade publishingFacade = new PublishingsFacade();
                Event events = (Event)Session["Event"];
                Address eventAddress = (Address)Session["EventAddress"];
                UpdateModel(eventAddress, collection);
                Event currEvent = publishingFacade.GetEvent(events.Id);
                Address currAddress = publishingFacade.GetAddress(eventAddress.Id);
                currAddress = eventAddress;
                currEvent = events;
                publishingFacade.UpdateEvent(currEvent, currAddress);

                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Event/Delete/5
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Delete(int id)
        {
            return View(publishingFacade.GetEvent(id));
        }

        //
        // POST: /Event/Delete/5

        [HttpPost]
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                publishingFacade.DeleteEvent(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(publishingFacade.GetEvent(id));
            }
            catch (Exception ex)
            {
                return View(publishingFacade.GetEvent(id));
            }
        }
    }


    public class ArticleController : Controller
    {
        PublishingsFacade publishingFacade = null;
        public ArticleController()
        {
            publishingFacade = new PublishingsFacade();
        }

        //
        // GET: /Article/

        public ActionResult Index()
        {
            if (Common.IsLoggedInUserAdmin())
                return View(publishingFacade.FindAllArticles());
            else
                return View(publishingFacade.FindAllLatestArticles());
        }

        //
        // GET: /Article/Details/5

        public ActionResult Details(int id)
        {
            return View(publishingFacade.GetArticle(id));
        }

        //
        // GET: /Article/Create
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Article/Create

        [HttpPost]
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Article article = new Article();
                UpdateModel(article, collection);
                article.UserId = Common.GetLoggedInUserId();
                publishingFacade.AddArticle(article);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Article/Edit/5
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Edit(int id)
        {
            return View(publishingFacade.GetArticle(id));
        }

        //
        // POST: /Article/Edit/5

        [HttpPost]
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Article article = publishingFacade.GetArticle(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(article);
                publishingFacade.UpdateArticle(article);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(article);
            }
            catch (Exception ex)
            {
                return View(article);
            }
        }

        //
        // GET: /Article/Delete/5
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Delete(int id)
        {
            return View(publishingFacade.GetArticle(id));
        }

        //
        // POST: /Article/Delete/5

        [HttpPost]
        [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                publishingFacade.DeleteArticle(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(publishingFacade.GetArticle(id));
            }
            catch (Exception ex)
            {
                return View(publishingFacade.GetArticle(id));
            }
        }
    }

    [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
    public class AddressController : Controller
    {
        PublishingsFacade publishingFacade = null;
        public AddressController()
        {
            publishingFacade = new PublishingsFacade();
        }

        //
        // GET: /Address/

        public ActionResult Index()
        {
            return View(publishingFacade.FindAllAddresses());
        }

        //
        // GET: /Address/Details/5

        public ActionResult Details(int id)
        {
            return View(publishingFacade.GetAddress(id));
        }

        //
        // GET: /Address/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Address/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Address address = new Address();
                UpdateModel(address, collection);
                publishingFacade.AddAddress(address);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Address/Edit/5

        public ActionResult Edit(int id)
        {
            return View(publishingFacade.GetAddress(id));
        }

        //
        // POST: /Address/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Address address = publishingFacade.GetAddress(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(address);
                publishingFacade.UpdateAddress(address);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(address);
            }
            catch (Exception ex)
            {
                return View(address);
            }
        }

        //
        // GET: /Address/Delete/5

        public ActionResult Delete(int id)
        {
            return View(publishingFacade.GetAddress(id));
        }

        //
        // POST: /Address/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                publishingFacade.DeleteAddress(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(publishingFacade.GetAddress(id));
            }
            catch (Exception ex)
            {
                return View(publishingFacade.GetAddress(id));
            }
        }
    }
}

